package demo.suanfa;

public class _二维数组 {
    public static void main(String[] args) {
        //1 2 3 4
        //5 6 7 8
        //9 10 11 12
        //13 14 15 16
        int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
//        m1(arr);
        m2();
    }

    private static void m2() {
        // 创建一个3x4的二维数组（矩阵），并用1到12的数字按行初始化它
        //按行打印矩阵。
        //按列打印矩阵。
        //打印矩阵的对角线元素（假设是方阵，本题可先扩展为4x4矩阵）
        int l=3,r=4;
        int[][] arr = new int[l][r];
        int k = 1;
        for (int i = 0; i < l; i++) {
            for (int j = 0; j < r; j++) {
                arr[i][j] = k++;
            }
        }
        // 按行打印
        for (int i = 0; i < l; i++) {
            for (int j = 0; j < r; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("==========");
        // 按列打印
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < l; j++) {
                System.out.print(arr[j][i] + " ");
            }
            System.out.println();
        }
        System.out.println("================");

        System.out.println();
    }

    private static void m1(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            // 打印左对角线
            System.out.print(arr[i][i] + ",");
        }
        System.out.println();
        for (int i = 0; i < arr.length; i++) {
            // 右对角线
            System.out.print(arr[i][arr[i].length - i -1] + ",");
        }
    }
}
